<?php

global $Application_Maker, $oThis;
if (!class_exists('setSelectMailTplCls')) {

    class setSelectMailTplCls extends Application_Maker_setField {

        public function __construct() {
            $this->fieldtype = "setSelectMailTpl";
            $this->hasSaveField = true;
            $this->AbortGlobalSave = false;
            $this->classSubActions = array( 'getMailTpl' , 'getMailTplDetail');
        }

        public function getMailTpl() {
            global $wpdb, $current_user, $meta_marker, $post_id;
			// $args = array(
				// 'numberposts' => -1,
				// 'post_type' => 'ff_email_template',
			// );
			// $results = get_posts($args);
			$uid = $current_user->ID;
			if($uid == 1){
				$str = "SELECT post_title, post_name , ID
						FROM wp_posts 
						WHERE post_type = 'ff_email_template' AND post_status = 'publish'";
			}else{
				$public_str = "SELECT wp_terms.term_id
							FROM wp_term_taxonomy , wp_terms
							WHERE taxonomy = 'cat15_emailtpl_privacy' AND wp_terms.term_id = wp_term_taxonomy.term_id AND slug = 'public'";
				$public_results = $wpdb->get_results($public_str);
				$public_id = $public_results[0]->term_id;
				
				$private_str = "SELECT wp_terms.term_id
							FROM wp_term_taxonomy , wp_terms
							WHERE taxonomy = 'cat15_emailtpl_privacy' AND wp_terms.term_id = wp_term_taxonomy.term_id AND slug = 'private-2'";
				$private_results = $wpdb->get_results($private_str);
				$private_id = $private_results[0]->term_id;
				
				$str = "SELECT DISTINCT post_title, post_name , ID
						FROM wp_posts 
						LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
						WHERE post_type = 'ff_email_template' AND post_status = 'publish'
						AND ((post_author = $uid AND wp_postmeta.meta_key = 'privacy' AND wp_postmeta.meta_value = $private_id) OR (wp_postmeta.meta_key = 'privacy' AND wp_postmeta.meta_value = $public_id))
						";
				
			}
            $results = $wpdb->get_results($str);
			
            echo json_encode($results);
        }
		public function getMailTplDetail() {
            global $wpdb, $current_user, $meta_marker, $post_id;
			$str = "SELECT DISTINCT wp_posts.ID ,wp_posts.post_title , wp_postmeta.meta_value, wp_postmeta.meta_key
					FROM wp_posts , wp_postmeta
					WHERE post_type = 'ff_email_template' AND post_status = 'publish' AND wp_posts.ID = wp_postmeta.post_id
					AND (wp_postmeta.meta_key = 'email_subject' OR wp_postmeta.meta_key = 'email_body')
					AND wp_posts.ID = ".$_POST['ID_tplMail']."
					";
			$results = $wpdb->get_results($str);
			if($results){
				$resultArray = array();
				array_push($resultArray, array( 
					"ID" =>$_POST['ID_tplMail'],										
					"post_title" =>$results[0]->post_title,										
					$results[0]->meta_key => $results[0]->meta_value,										
					$results[1]->meta_key => $results[1]->meta_value,										
				));
				
				echo json_encode($resultArray);
			}
			echo false;
		}

        public function saveField($post_id, $key, $meta_marker, $data, $field) {
            global $current_user, $post_id;
            $destinees = $_POST[$key . $meta_marker];
            if ($send == "send") {//if send = start the email sending process...
            }
            update_post_meta($post_id,$key . $meta_marker,$destinees);
           // mail_compose_to mail_compose_cc
           // var_dump($_REQUEST);
           // exit;
        }

        public function getField($oThis, $config, $post, $meta_marker) {
            global $current_user, $post_id;
            //'test'.$this->meta_marker."**".$this->add_image;


            $this->init($oThis, $config, $post, $meta_marker);
            // print_r($oThis);exit();
            $str = "<div class='c_field_container c_setSelectMailTpl' data-field_type='setSelectMailTpl'
                     data-fwidthCls='" . $config['fwidthCls'] . "'
                     data-field='" . $config['field'] . "'
                     data-value ='" . $config['value'] . "'
                     data-label ='" . $config['label'] . "'
                     data-meta_marker='" . $meta_marker . "'></div>";
            return $str; //'test'.$this->meta_marker."**".$this->add_image;
            return $str;
        }

    }

}

$oThis->extension_class_instances['setSelectMailTpl'] = new setSelectMailTplCls();
//$setUploadGrid=new setUploadGridCls();
?>
